﻿using System;
using System.ComponentModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sovacode.RubiksCube.Common.Managers.Validation;
using Sovacode.RubiksCube.Common.ViewModel;

namespace Sovacode.RubiksCube.Common.Tests.ValidationManager
{
    [TestClass]
    public class CommonValidationManagerTests
    {
        [TestMethod]
        public void AddValidation()
        {
            var viewModelTop = new ViewModelTop();

            viewModelTop.RegisterRule(() => viewModelTop.CommandsCount, () => viewModelTop.CommandsCount == 0);

            Assert.IsTrue(viewModelTop.IsValid);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == viewModelTop.ValidatorsCount);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == 1);

            viewModelTop.RegisterRule("SettingsCount", () => viewModelTop.SettingsCount == 0);

            Assert.IsTrue(viewModelTop.IsValid);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == viewModelTop.ValidatorsCount);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == 2);

            viewModelTop.RegisterRule("ValidatorsCount", new ValidationRule { Validate =  () => viewModelTop.ValidatorsCount == 3});

            Assert.IsTrue(viewModelTop.IsValid);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == viewModelTop.ValidatorsCount);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == 3);

        }

        [TestMethod]
        public void Validate()
        {
            var viewModelTop = new ViewModelTop();

            viewModelTop.RegisterRule(() => viewModelTop.CommandsCount, () => viewModelTop.CommandsCount == 0);

            Assert.IsTrue(viewModelTop.IsValid);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == viewModelTop.ValidatorsCount);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == 1);

            viewModelTop.RegisterCommand("SimpleCommand", o => { });

            Assert.IsFalse(viewModelTop.IsValid);

            Assert.IsFalse(string.IsNullOrWhiteSpace(viewModelTop.ValidationManager.Validate(() => viewModelTop.CommandsCount)));
        }

        [TestMethod]
        public void ValidateByIndex()
        {
            var viewModelTop = new ViewModelTop();

            viewModelTop.RegisterRule(() => viewModelTop.CommandsCount, () => viewModelTop.CommandsCount == 0);

            Assert.IsTrue(viewModelTop.IsValid);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == viewModelTop.ValidatorsCount);
            Assert.IsTrue(viewModelTop.ValidValidatorsCount == 1);

            var errorInfo = (viewModelTop as IDataErrorInfo);

            Assert.IsNotNull(errorInfo);

            Assert.IsTrue(string.IsNullOrWhiteSpace(errorInfo["CommandsCount"]));
        }

        [TestMethod]
        public void BadValidation()
        {
            var viewModelTop = new ViewModelTop();

            Assert.IsFalse(string.IsNullOrWhiteSpace((viewModelTop as IDataErrorInfo)["BadValidation"]));

            Assert.IsTrue(viewModelTop.InvalidValidatorsCount == viewModelTop.ValidatorsCount);
            Assert.IsTrue(viewModelTop.InvalidValidatorsCount == 1);
            
            Assert.IsTrue(viewModelTop.InvalidValidators.Contains("BadValidation"));
        }
    }
}
